/**
 * Copyright (c) 2011 - 2012,
 * @fileoverview Sina  表单-extUrl基础组件(用于type=url的input控件)
 * @author  bowenliu082 | bowenliu082@163.com
 * @version 1.0 | 2012-2-20
 * @param opt{
 * 	'el': '',       // input标签
 *  'require':['focus','blur']	//是否需要调用focus blur函数
 * }
 * form_data="isMust=1&isChange=1regPara=^abc$"
 * @example
 * var a = new STK.cpt.extend.extUrl(opt);
 */
$require('form.cFormTips');
$require('form.formInputText');
LJS.define('form.cFormUrl', function(ljs){
    var extUrlTpl = {
		tip:'请正确填写url',
		error:'请正确填写url'
	}
    function extUrl(opt){
        var that = this;
        if (!opt || !opt.el) 
            return false;
        that.el = opt.el;
		that.tipsEl = opt.tipsEl || opt.el;   // 要弹出的提示层位置
        that.require = opt.require || [];
        that.value = that.el.value || null;
        var formData = that.el.getAttribute('form_data');
        that.data = typeof formData === 'string' ? $.queryToJson(formData) : {};
           // 是否实时判断合法性
        that.changeStore = parseInt(that.data.isChange) || 0;
        that.isChange = 0;
		that.state = false;
        that.isMust = parseInt(that.data.isMust) || 0;
        that.regPara = that.data.regPara || '';//要求传入的为正则表达式包括 '^'
        that.init();
    }
    extUrl.prototype = new ljs.form.formInputText;
    extUrl.prototype.init = function(){
        var that = this;
        that.value !== null ? that.setValue(that.value) : '';
		 that.tip = new ljs.form.cFormTips({
            'el': that.tipsEl || that.el
        });
        $.foreach(that.require, function(a, i){
            switch (a) {
                case 'focus':
                    that.focus({
                        callback: function(txtLength){
                           that.state === true ? '' : that.showTip();
                        },
                        loop: function(txtLength){
                           that.check() ? that.hide() : that.showError();
                        }
                    });
                    break;
                case 'blur':
                    that.blur({
                        callback: function(){
                            if (that.check()) {
                                that.isChange = 0;
                                that.hide();
                                that.state = false;
								that.setValue(that.getValue());
                            }
                            else {
                                that.showError();
								$.addClassName(that.el, 'error');
                                that.isChange = that.changeStore;
                                that.state = true;
                            }
                        }
                        
                        
                    });
                    break;
                default:
                    return false;
            }
        });
    };
    /**
     * 检测Url是否符合要求
     */
    extUrl.prototype.check = function(){
        var that = this;
        var urlValue = that.getValue();
        if (that.isMust) {
            if (that.isEmpty()) {
                return false;
            }
			if(that.getValue()==that.el.getAttribute('defValue')){
				return false
			}
            if (that.regPara) {
                var _reg = new RegExp(that.regPara, "ig");
                return _reg.test(urlValue);
            }
            var _regPara = /^(https|http|ftp|rtsp|mms):\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/i;
			//console.log(_regPara.test(urlValue))
            return _regPara.test(urlValue);
        }
        else {
			 if (that.isEmpty()) {
                return  true;
            }
            if (that.regPara) {
				var _reg = new RegExp(that.regPara, "ig");
				 return _reg.test(urlValue);
			}
            if (urlValue == 'http://') {
                return true;
            }
            
            var _regPara = /^(https|http|ftp|rtsp|mms):\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/i;
			
            return _regPara.test(urlValue);
        }
        
    };
    
    // 显示提示层
    extUrl.prototype.showTip = function(){
      var that = this;
      that.tip.show(extUrlTpl.tip);
    };
    // 隐藏提示层
    extUrl.prototype.hide = function(){
    	var that = this;
		$.removeClassName(that.el, 'error');
       that.tip.hide();
    };
    // 显示错误提示层
    extUrl.prototype.showError = function(){
     var that = this;
	 	$.addClassName(that.el, 'error');
	 	that.tip.show(extUrlTpl.error,1);
    };
    
    
    /**
     * 设置form_datad 值
     * 	J2Q();
     */
    extUrl.prototype.J2Q = function(){
        var that = this;
        var opt = {
        	'regPara':that.regPara||'',
            'isChange': that.isChange || '',
            'isMust': that.isMust || ''
        }
        that.data = $.jsonToQuery(opt);
        that.el.setAttribute('form_data', that.data);
    };
    return extUrl;
})
